package com.schx.budget.service.impl

import com.schx.budget.pojo.BudgetTargetUpdate
import com.schx.budget.mapper.BudgetTargetMapper
import com.schx.budget.pojo.Budget
import com.schx.budget.pojo.BudgetTarget
import com.schx.budget.pojo.BudgetType
import com.schx.budget.service.BudgetTargetService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.text.SimpleDateFormat
import java.util.*

@Service
class BudgetTargetServiceImpl: BudgetTargetService{

    @Autowired
    lateinit var budgetTargetMapper: BudgetTargetMapper

    override fun getTargetList(type: String, orgid: String): List<BudgetTarget> {
        val cal = Calendar.getInstance()
        val simple = SimpleDateFormat("yyyy-MM")
        cal.add(Calendar.MONTH, -1)
        var lastMonth = simple.format(cal.time)
        return budgetTargetMapper.getTargetList(type, lastMonth,orgid)
    }

    override fun getCostTargetList(type: String, orgid: String): List<BudgetTarget> {
        val cal = Calendar.getInstance()
        var lastYear = cal.get(Calendar.YEAR) - 1
        if (type.substring(0,1).equals("1")){
            return budgetTargetMapper.getInTargetList(type,lastYear.toString(),orgid)
        }else{
            return budgetTargetMapper.getCostTargetList(type,orgid)
        }
    }

    override fun save(budgetTarget: BudgetTarget): Int {
        if(budgetTarget.orgid == null){
            budgetTarget.orgid = 1001
        }
        var checkCount = budgetTargetMapper.checkContent(budgetTarget.type1.toString(), budgetTarget.typeId.toString(), budgetTarget.orgid.toString())
        if (checkCount != null && checkCount > 0) {
            return -1
        }
        var id: String?
        if (budgetTarget.id == null) {
            id = budgetTarget.id
        }else {
            id = UUID.randomUUID().toString().replace("-","")
        }
        var targetUpdate = BudgetTargetUpdate(
                id,
                budgetTarget.id,
                budgetTarget.amount_goal,
                budgetTarget.type.toString(),
                budgetTarget.orgid.toString(),
                budgetTarget.month1,
                budgetTarget.month2,
                budgetTarget.month3,
                budgetTarget.month4,
                budgetTarget.month5,
                budgetTarget.month6,
                budgetTarget.month7,
                budgetTarget.month8,
                budgetTarget.month9,
                budgetTarget.month10,
                budgetTarget.month11,
                budgetTarget.month12,
                Date(),
                "测试")
        budgetTarget.create_date = Date()
        budgetTarget.update_date = Date()
        budgetTargetMapper.insert(targetUpdate)
        if(budgetTarget.id != "") {
            return budgetTargetMapper.update(budgetTarget)
        }else{
            //如果是毛利--保存type表
            if(budgetTarget.type.toString().substring(0,1).equals("1")) {
                var budgetTypr = BudgetType(budgetTarget.typeId, budgetTarget.type1, budgetTarget.type2,budgetTarget.type3)
                budgetTargetMapper.insertType(budgetTypr)
            }
            return budgetTargetMapper.save(budgetTarget)
        }
    }

    override fun getProfits(orgid: String): MutableMap<String, Any?> {
        var map = HashMap<String, Any?>()
        var list = budgetTargetMapper.getSumProfits(orgid)
        var list1 = list.filter { it.project.equals("费用")}
        var costAmount = list1.stream().mapToDouble({ item -> item.budgetAmount }).sum()
        var list2 = list.filter { it.project.equals("毛利")}
        var proAmount = list2.stream().mapToDouble({ item -> item.budgetAmount }).sum()
        var wageList = budgetTargetMapper.getWage(orgid)
        var userCountList = budgetTargetMapper.getPersonNum(orgid)
        map.put("costAmount", costAmount)
        map.put("proAmount", proAmount)
        map.put("policy", budgetTargetMapper.getSumPolicy(orgid))
        map.put("wage", wageList.stream().mapToInt({item -> item}).sum())
        map.put("userCount", userCountList.stream().mapToInt({item -> item}).sum())
        map.put("list", budgetTargetMapper.getProfits(orgid))
        return map
    }

    override fun delTarget(orgid: String, id: String): Int {
        var orgid = orgid
        if(orgid.equals("")){
            orgid = "1001"
        }
        return budgetTargetMapper.delTarget(orgid, id)
    }
}


